package com.onlylemi.mapview.library.layer;

import com.onlylemi.mapview.library.MapView;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Point;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

/**
 * BitmapLayer
 *
 * @author: onlylemi
 */
public class BitmapLayer extends MapBaseLayer {

    private Point location;
    private PixelMap bitmap;
    private Paint paint;

    private boolean autoScale = false;

    private OnBitmapClickListener onBitmapClickListener;

    public BitmapLayer(MapView mapView, PixelMap bitmap) {
        this(mapView, bitmap, null);
    }

    public BitmapLayer(MapView mapView, PixelMap bitmap, Point location) {
        super(mapView);
        this.location = location;
        this.bitmap = bitmap;

        paint = new Paint();
    }

    @Override
    public void onTouch(TouchEvent event) {
        if (onBitmapClickListener != null) {
            float[] goal = mapView.convertMapXYToScreenXY(
                    event.getPointerPosition(0).getX(), event.getPointerPosition(0).getY());
            if (goal[0] > location.getPointX() - (float) bitmap.getImageInfo().size.width / 2
                    && goal[0] < location.getPointX() + (float) bitmap.getImageInfo().size.width / 2
                    && goal[1] > location.getPointY() - (float) bitmap.getImageInfo().size.height / 2
                    && goal[1] < location.getPointY() + (float) bitmap.getImageInfo().size.height / 2) {
                onBitmapClickListener.onBitmapClick(this);
            }
        }
    }

    @Override
    public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float
            currentRotateDegrees) {
        if (isVisible && bitmap != null) {
            canvas.save();
            float[] goal = {location.getPointX(), location.getPointY()};
            if (!autoScale) {
                currentMatrix.mapPoints(goal);
            } else {
                canvas.setMatrix(currentMatrix);
            }
            canvas.drawPixelMapHolder(new PixelMapHolder(bitmap), goal[0] - (float) bitmap.getImageInfo().size.width / 2,
                    goal[1] - (float) bitmap.getImageInfo().size.width / 2, paint);
            canvas.restore();
        }
    }

    public Point getLocation() {
        return location;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public PixelMap getBitmap() {
        return bitmap;
    }

    public void setBitmap(PixelMap bitmap) {
        this.bitmap = bitmap;
    }

    public void setAutoScale(boolean autoScale) {
        this.autoScale = autoScale;
    }

    public boolean isAutoScale() {
        return autoScale;
    }

    public void setOnBitmapClickListener(OnBitmapClickListener onBitmapClickListener) {
        this.onBitmapClickListener = onBitmapClickListener;
    }

    public interface OnBitmapClickListener {
        void onBitmapClick(BitmapLayer layer);
    }
}
